package com.sun.controller;

import com.github.pagehelper.PageInfo;
import com.sun.enums.HttpEnum;
import com.sun.pojo.dto.EmployeesLoginDTO;
import com.sun.pojo.dto.EmployeesRegisterDTO;
import com.sun.pojo.entity.Employees;
import com.sun.pojo.vo.R;
import com.sun.service.EmployeesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * (Employees)表控制层
 *
 * @author sun
 * @since 2024-10-21 10:00:28
 */
@RestController
@RequestMapping("employees")
@CrossOrigin
public class EmployeesController{
    /**
     * 服务对象
     */
    @Autowired
    EmployeesService employeesService;

    /**
     * 分页查询所有数据
     *
     * @param pageNum 分页对象
     * @param employees 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R<PageInfo<Employees>> selectAll(Employees employees, Integer pageNum) {
        PageInfo<Employees> employeesPageInfo = employeesService.selectAllByPage(employees, pageNum);
        return R.success(employeesPageInfo);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Integer id) {
        Employees employees = employeesService.selectByEmployeeId(id);
        return R.success(employees);
    }

    /**
     * 新增数据
     *
     * @param employees 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody Employees employees) {
        if (employeesService.insertSelective(employees)>0){
            return R.success("新增成功");
        }else {
            return R.fail(HttpEnum.WARN.getCode(),"新增失败");
        }
    }

    /**
     * 修改数据
     *
     * @param employees 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Employees employees) {
        if (employeesService.updateByEmployeeId(employees,employees.getEmployeeId())>0){
            return R.success("修改成功");
        }else {
            return R.fail(HttpEnum.WARN.getCode(),"修改失败");
        }
    }

    /**
     * 删除数据
     *
     * @param employeeId 主键结合
     * @return 删除结果
     */
    @DeleteMapping("{id}")
    public R delete(@PathVariable("id") Integer employeeId) {
        if (employeesService.deleteByEmployeeId(employeeId)>0){
            return R.success("删除成功");
        }else {
            return R.fail(HttpEnum.WARN.getCode(),"删除失败");
        }
    }

    /**
     * 登录
     *
     * @param employeesLogin 用户
     * @return 删除结果
     */
    @PostMapping("login")
    public R login(@RequestBody EmployeesLoginDTO employeesLogin) {
        return employeesService.selectByLoginName(employeesLogin.getUsername(),employeesLogin.getPassword());
    }

    /**
     * 注册
     *
     * @param employeesRegisterDTO 用户
     * @return 删除结果
     */
    @PostMapping("register")
    public R register(@RequestBody EmployeesRegisterDTO employeesRegisterDTO) {
        if (employeesService.registerEmployees(employeesRegisterDTO)>0){
            return R.success("注册成功");
        }else {
            return R.fail(HttpEnum.WARN.getCode(),"注册失败");
        }
    }

}

